```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx限流与延迟处理指南 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            margin-top: 1.5em;
            margin-bottom: 0.8em;
        }
        .hero {
            background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 100%);
            color: white;
        }
        .code-block {
            background-color: #f8f8f8;
            border-radius: 8px;
            padding: 1.5em;
            overflow-x: auto;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .highlight {
            background-color: #f0f9ff;
            border-left: 4px solid #3b82f6;
        }
        .mermaid {
            background-color: white;
            padding: 2em;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            margin: 2em 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-4xl text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6">Nginx限流与延迟处理指南</h1>
            <p class="text-xl md:text-2xl opacity-90 mb-8">掌握服务器负载控制的关键技术</p>
            <div class="flex justify-center space-x-4">
                <a href="#rate-limiting" class="px-6 py-3 bg-white text-blue-900 font-semibold rounded-full hover:bg-opacity-90 transition-all">
                    <i class="fas fa-tachometer-alt mr-2"></i>限流技术
                </a>
                <a href="#delay-handling" class="px-6 py-3 bg-opacity-20 bg-white rounded-full hover:bg-opacity-30 transition-all">
                    <i class="fas fa-clock mr-2"></i>延迟处理
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-4xl px-4 md:px-0 py-12">
        <article class="prose lg:prose-xl max-w-none">
            <p class="lead">在 Nginx 中实现对特定请求的限流和延迟处理可以帮助控制服务器的负载，防止滥用，并确保服务的可用性。</p>

            <!-- Rate Limiting Section -->
            <section id="rate-limiting" class="mt-16">
                <h2 class="text-3xl font-bold border-b-2 border-blue-100 pb-2"><i class="fas fa-tachometer-alt text-blue-600 mr-3"></i>限流（Rate Limiting）</h2>
                <p>限流可以帮助你限制客户端在一定时间内能够发送的请求数量，从而防止过多的请求压垮服务器。</p>

                <!-- Limit Req Module -->
                <div class="mt-8">
                    <h3 class="text-2xl font-semibold flex items-center">
                        <span class="bg-blue-100 text-blue-800 rounded-full w-8 h-8 flex items-center justify-center mr-3">1</span>
                        使用 <code>limit_req</code> 模块
                    </h3>
                    <p><code>limit_req</code> 模块用于限制请求速率。配置时需要定义一个速率限制区域，然后将其应用到特定的 location 或 server。</p>
                    
                    <div class="code-block mt-4">
                        <pre>http {
    # 定义限制区域
    limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;

    server {
        location /api/ {
            # 应用限流
            limit_req zone=mylimit burst=20 nodelay;
            proxy_pass http://backend;
        }
    }
}</pre>
                    </div>
                    
                    <div class="highlight p-4 mt-4 rounded-lg">
                        <ul class="list-disc pl-5 space-y-2">
                            <li><code>limit_req_zone</code>: 定义一个名为 <code>mylimit</code> 的限制区域，限制速率为每秒 10 个请求。</li>
                            <li><code>limit_req</code>: 应用限流，<code>burst</code> 参数允许突发的请求数，<code>nodelay</code> 表示立即拒绝超出限流的请求。</li>
                        </ul>
                    </div>
                </div>

                <!-- Limit Conn Module -->
                <div class="mt-12">
                    <h3 class="text-2xl font-semibold flex items-center">
                        <span class="bg-blue-100 text-blue-800 rounded-full w-8 h-8 flex items-center justify-center mr-3">2</span>
                        使用 <code>limit_conn</code> 模块
                    </h3>
                    <p><code>limit_conn</code> 模块用于限制每个 IP 地址的连接数量。</p>
                    
                    <div class="code-block mt-4">
                        <pre>http {
    # 定义连接限制区域
    limit_conn_zone $binary_remote_addr zone=conn_limit:10m;

    server {
        location / {
            limit_conn conn_limit 5;  # 限制每个 IP 地址最多 5 个连接
            proxy_pass http://backend;
        }
    }
}</pre>
                    </div>
                    
                    <div class="highlight p-4 mt-4 rounded-lg">
                        <ul class="list-disc pl-5 space-y-2">
                            <li><code>limit_conn_zone</code>: 定义一个名为 <code>conn_limit</code> 的连接限制区域。</li>
                            <li><code>limit_conn</code>: 应用连接限制，每个 IP 地址最多 5 个连接。</li>
                        </ul>
                    </div>
                </div>

                <!-- Visualization -->
                <div class="mermaid mt-12">
                    graph TD
                        A[客户端请求] --> B{Nginx限流检查}
                        B -->|在限制内| C[处理请求]
                        B -->|超出限制| D[拒绝/延迟请求]
                        C --> E[后端服务]
                        D --> F[返回429错误]
                </div>
            </section>

            <!-- Delay Handling Section -->
            <section id="delay-handling" class="mt-20">
                <h2 class="text-3xl font-bold border-b-2 border-blue-100 pb-2"><i class="fas fa-clock text-blue-600 mr-3"></i>延迟处理（Delay Handling）</h2>
                <p>延迟处理可以用来模拟网络延迟，控制请求的处理速度，或者在流量高峰期时对请求进行排队。</p>

                <!-- Limit Req with Delay -->
                <div class="mt-8">
                    <h3 class="text-2xl font-semibold flex items-center">
                        <span class="bg-blue-100 text-blue-800 rounded-full w-8 h-8 flex items-center justify-center mr-3">1</span>
                        使用 <code>limit_req</code> 模块实现延迟处理
                    </h3>
                    <p>通过 <code>limit_req</code> 模块的 <code>burst</code> 参数，可以在超出限流限制时实现延迟处理。</p>
                    
                    <div class="code-block mt-4">
                        <pre>http {
    limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s;

    server {
        location /api/ {
            limit_req zone=mylimit burst=10 delay;
            proxy_pass http://backend;
        }
    }
}</pre>
                    </div>
                    
                    <div class="highlight p-4 mt-4 rounded-lg">
                        <ul class="list-disc pl-5 space-y-2">
                            <li><code>delay</code>: 当请求速率超出限制时，<code>burst</code> 参数定义的额外请求将被延迟处理。</li>
                        </ul>
                    </div>
                </div>

                <!-- Custom Delay with Headers -->
                <div class="mt-12">
                    <h3 class="text-2xl font-semibold flex items-center">
                        <span class="bg-blue-100 text-blue-800 rounded-full w-8 h-8 flex items-center justify-center mr-3">2</span>
                        使用 <code>add_header</code> 模块结合 <code>limit_req</code> 实现自定义延迟响应
                    </h3>
                    <p>你可以使用 <code>add_header</code> 模块结合 <code>limit_req</code> 模块来模拟延迟。</p>
                    
                    <div class="code-block mt-4">
                        <pre>http {
    limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;

    server {
        location /api/ {
            limit_req zone=mylimit burst=20;
            proxy_pass http://backend;
            # 模拟延迟响应
            add_header X-Delay-Response "Delay for 1 second";
            add_header X-Rate-Limit-Remaining $limit_req_limit;
        }
    }
}</pre>
                    </div>
                    
                    <div class="highlight p-4 mt-4 rounded-lg">
                        <ul class="list-disc pl-5 space-y-2">
                            <li><code>add_header</code>: 可以添加自定义的头部信息来模拟延迟效果。</li>
                        </ul>
                    </div>
                </div>

                <!-- Best Practices Card -->
                <div class="mt-12 grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div class="card bg-white p-6 rounded-lg border border-gray-100">
                        <div class="text-blue-600 text-2xl mb-4">
                            <i class="fas fa-lightbulb"></i>
                        </div>
                        <h4 class="text-xl font-semibold mb-3">最佳实践</h4>
                        <ul class="space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>在生产环境实施渐进式限流策略</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>根据业务需求调整burst大小</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>监控限流指标以优化配置</span>
                            </li>
                        </ul>
                    </div>
                    
                    <div class="card bg-white p-6 rounded-lg border border-gray-100">
                        <div class="text-blue-600 text-2xl mb-4">
                            <i class="fas fa-exclamation-triangle"></i>
                        </div>
                        <h4 class="text-xl font-semibold mb-3">注意事项</h4>
                        <ul class="space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                <span>避免设置过于严格的限流策略</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                <span>考虑使用共享内存大小</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                <span>测试不同场景下的限流效果</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </section>
        </article>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8">
        <div class="container mx-auto max-w-4xl px-4 md:px-0">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-semibold mb-2">技术小馆</h3>
                    <p class="text-gray-400">专业的Web技术知识分享平台</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="hover:text-blue-300 transition-colors">
                        <i class="fas fa-external-link-alt mr-2"></i>访问技术小馆
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-6 pt-6 text-center text-gray-400 text-sm">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```